<?php
    require_once("config.php");

    if ($_POST) {
        $fav = "SELECT * FROM favorites WHERE userid=" . AuthManager::GetUserID() . " AND listingid=" . $_POST['listingid'] . ";";
        if (DBM::CountRows($fav) > 0) {
            $query = "DELETE FROM favorites WHERE userid=" . AuthManager::GetUserID() . " AND listingid=" . $_POST['listingid'] . ";";
            DBM::Execute($query);
            echo $_POST['listingid'] . ":" . ThemeManager::GetImage("favorite_bw");
        }
        else {
            $query = "INSERT INTO favorites (userid,listingid) VALUES (" . AuthManager::GetUserID() . "," . $_POST['listingid'] . ");";
            DBM::Execute($query);
            echo $_POST['listingid'] . ":" . ThemeManager::GetImage("favorite");
        }
    }
    else {
        Functions::PrintHeader("Favorite Listings");
        $query = "SELECT * FROM favorites WHERE userid=" . AuthManager::GetUserID();
        $favorites = DBM::FetchAllRows($query);
        if (count($favorites) == 0) {
            InfoMessage::Show("No Favorites","You don't have any favorites");
        }
        else {
            foreach ($favorites as $favorite) {
                $listing = new ListingRow($favorite['listingid']);
                $listing->Render();
            }
        }
        Functions::PrintFooter();
    }
?>
